//
//  CompletedVc.m
//  Wy_BuyBooks
//
//  Created by wangyang on 2020/2/6.
//  Copyright © 2020 wangyang. All rights reserved.
//

#import "CompletedVc.h"
#import "CompletedVcCell.h"
#import "EvaluationVc.h"
#import "WyLnadViewController.h"
@interface CompletedVc ()<UITableViewDelegate,UITableViewDataSource,CompletedVcCellDele>

@property (nonatomic,strong) UITableView     *WyHomeTableView;
@property (nonatomic,strong) NSMutableArray     *WyOrderVcArray;
@property (nonatomic,strong) NSArray           *WyOrderVcarray;

@end

@implementation CompletedVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initSetArray];
    
    self.view.backgroundColor = WyWhiteColor;
     [self WyHomeTableView];
}
-(void)initSetArray
{


    _WyOrderVcArray = [[NSMutableArray alloc]init];

          NSMutableArray  *Order = [[NSUserDefaults standardUserDefaults]objectForKey:@"CompletedVclist"];

          if (Order == nil && Order.count == 0) {
              _WyOrderVcarray = @[
                                  @{@"order":@"订单编号：189892716",
                                  @"img":@"罗生门",
                                  @"name":@"罗生门",
                                  @"UserName":@"芥川龙之介",
                                  @"Score":@"综合评分：4.8",
                                  @"money":@"¥56.00",
                                  @"all":@"共1件商品  合计：¥90.00",
                                  @"people":@"收货人：李牛",
                                 @"phpne":@"联系方式： 13578265121",
                                  @"add":@"收货地址：河南省郑州市金水区国贸大厦",
                                  @"time":@"送达时间：2019-09-11 14:00"}];
              NSMutableArray  *nsmArray = [[NSMutableArray alloc]init];
              for (int i = 0; i<_WyOrderVcarray.count; i++) {
                  [nsmArray addObject:_WyOrderVcarray[i]];
              }
              [[NSUserDefaults standardUserDefaults]setValue:nsmArray forKey:@"CompletedVclist"];
          }
          NSMutableArray *nsm = [[NSUserDefaults standardUserDefaults]objectForKey:@"CompletedVclist"];
          [_WyOrderVcArray removeAllObjects];
          for (int i=0; i<nsm.count; i++) {
              [_WyOrderVcArray addObject:nsm[i]];
          }

}


-(UITableView *)WyHomeTableView

{
    
    if (!_WyHomeTableView) {
        
        
        _WyHomeTableView = [[UITableView alloc]init];
           _WyHomeTableView.delegate = self;
           _WyHomeTableView.dataSource = self;
           _WyHomeTableView.showsVerticalScrollIndicator = YES;
           _WyHomeTableView.backgroundColor = [UIColor clearColor];
           _WyHomeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
           _WyHomeTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(Shuaxin)];
           [self.view addSubview:_WyHomeTableView];
           [_WyHomeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
               make.left.right.top.bottom.mas_offset(0);
           }];
           [_WyHomeTableView.mj_header beginRefreshing];
           [_WyHomeTableView registerClass:[CompletedVcCell class] forCellReuseIdentifier:@"Cell"];
    }
    return _WyHomeTableView;
    
}
-(void)Shuaxin
{
    [self initSetArray];
    [self.WyHomeTableView reloadData];
    [self.WyHomeTableView.mj_header endRefreshing];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{
    return _WyOrderVcArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *CellId = @"Cell";
    
    CompletedVcCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
    
    if (!cell) {
        cell = [[CompletedVcCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    cell.IndexPath = indexPath;
    cell.PicImg.image = [UIImage imageNamed:_WyOrderVcArray[indexPath.row][@"img"]];
    cell.OrderLabe.text = _WyOrderVcArray[indexPath.row][@"order"];
    cell.NameLabe.text = _WyOrderVcArray[indexPath.row][@"name"];
    cell.ScoreLabe.text = _WyOrderVcArray[indexPath.row][@"Monthly"];
    cell.MoneyLabe.text = _WyOrderVcArray[indexPath.row][@"money"];
    cell.UserNameLabe.text = _WyOrderVcArray[indexPath.row][@"order"];
    
    return cell;
}


-(void)ContactMerchantClickIndexPath:(NSIndexPath *)IndexPath
{
    if ([WyEngineering sharedUser].login == YES) {

            [MyProgressHUD showWithMaskType:MyProgressHUDMaskTypeBlack];

                  //定时器
                  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                     
                    [MyProgressHUD dismiss];
                   
        
         [self.WyOrderVcArray removeObjectAtIndex:IndexPath.row];
        [self.WyHomeTableView reloadData];
                        
        [[AlertControllerCustom shareInstance] showAlert:@"订单已删除" viewController:self];
                  });
            
            
        }else{
            
            [MyProgressHUD showWithMaskType:MyProgressHUDMaskTypeBlack];

                         //定时器
                         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                            
                           [MyProgressHUD dismiss];
                          WyLnadViewController *VC = [[WyLnadViewController alloc] init];
                                              VC.LandVcBlock = ^{
                                                  //                        [self.MyTableView removeFromSuperview];
                                                  //                        self.MyTableView = nil;
                                                  self.tabBarController.selectedIndex = 0;
                                              };
                                              [self.navigationController pushViewController:VC animated:YES];
    //                         [self presentViewController:VC animated:YES completion:nil];
                         });
        
           
        }
   
}

-(void)CancelOrderClickIndexPath:(NSIndexPath *)IndexPath

{
    if ([WyEngineering sharedUser].login == YES) {

             [MyProgressHUD showWithMaskType:MyProgressHUDMaskTypeBlack];

                   //定时器
                   dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                      
                     [MyProgressHUD dismiss];
                    
         
         [self.navigationController pushViewController:[EvaluationVc alloc] animated:YES];
                   });
             
             
         }else{
             
             [MyProgressHUD showWithMaskType:MyProgressHUDMaskTypeBlack];

                          //定时器
                          dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                             
                            [MyProgressHUD dismiss];
                           WyLnadViewController *VC = [[WyLnadViewController alloc] init];
                                               VC.LandVcBlock = ^{
                                                   //                        [self.MyTableView removeFromSuperview];
                                                   //                        self.MyTableView = nil;
                                                   self.tabBarController.selectedIndex = 0;
                                               };
                                               [self.navigationController pushViewController:VC animated:YES];
     //                         [self presentViewController:VC animated:YES completion:nil];
                          });
         
            
         }
    
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return BiLiWidth(225);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
